<template>
  <el-dialog
    title="审核拒绝"
    :visible.sync="dialogVisible"
    width="50%"
    top="20vh"
    :before-close="closeHandle"
    :close-on-click-modal="false">
    <el-divider content-position="left">回复表单</el-divider>
    <el-form :model="rejectBlogForm" :rules="rejectBlogRules" ref="rejectBlogForm" @keyup.enter.native="rejectBlogHandle" label-width="85px">
      <el-form-item label="回复消息" prop="message">
        <el-input v-model="rejectBlogForm.message" type="textarea" maxlength="20"  placeholder="请输入回复用户消息"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeHandle(false)">关闭</el-button>
      <el-button :disabled="buttonLoad" type="primary" @click="rejectBlogHandle">拒绝</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    name: "reject-blog-dialog",
    props: {
      // 弹出框是否显示
      dialogVisible: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 博客信息对象
        blog: {},
        // 管理员回复用户消息表单
        rejectBlogForm: {
          message: ''
        },
        // 按钮是否可被点击[false-可以被点击 true-不可被点击]
        buttonLoad: false,
        rejectBlogRules: {
          message: [
            { required: true, message: '回复消息不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      // 初始化弹出框
      initBlog(blog) {
        console.log("boke = ", blog)
        this.blog = blog
      },
      // 下架博客事件
      rejectBlogHandle() {
        this.$refs['rejectBlogForm'].validate((valid) => {
          if (!valid) {
            return
          }

          this.$confirm(`您确定要拒绝发布'${this.blog.title}'博客吗?`, '拒绝确认', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {

            this.buttonLoad = true    // 按钮不可被点击

            let obj = {
              blogId: this.blog.blogId,
              userId: this.blog.userId,
              status: false,
              message: this.rejectBlogForm.message,
            }
            this.$axios.$post('/blog-manage/audit/result', obj)
              .then(() => {

                this.$notify({
                  title: '拒绝博客成功',
                  type: 'success',
                  message: `成功拒绝'${this.blog.title}'博客发布`,
                  offset: 100,
                  duration: 2000
                });

                this.closeHandle(true)
              })
              .catch(() => {})
              .finally(() => {
                // 按钮恢复可点击
                this.buttonLoad = false
              })

          }).catch(() => {})
        })

      },
      // 弹出框关闭处理事件
      closeHandle(bool) {
        this.$refs['rejectBlogForm'].resetFields()            // 清空系统用户表单信息
        this.blog = {}                                        // 手动清空博客对象

        if (typeof bool === 'function') {                     // 此处判断触发事件是否是弹出框自带关闭事件
          bool = false
        }
        this.$emit('close-reject-blog-dialog', bool)   // 向上传递事件
      },
    }
  }
</script>

<style scoped>

</style>
